package net.hserver.sync.entity;


import com.github.sardine.impl.SardineImpl;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import com.mybatisflex.core.keygen.KeyGenerators;
import lombok.Data;
import lombok.ToString;
import net.hserver.sync.config.SyncType;
import net.hserver.sync.file.FileMonitor;
import net.hserver.sync.util.PathUtil;

import java.io.File;
import java.util.Date;

@Data
@ToString
@Table("sync_webdav_config")
public class SyncWebDavConfigEntity {
    @Id(keyType = KeyType.Generator, value = KeyGenerators.flexId)
    private String id;
    private SyncType syncType;
    private SyncMode syncMode;
    private String syncTime;
    private String localPath;
    private String remotePath;
    private String webDavAddress;
    private String webDavUsername;
    private String webDavPassword;
    private Date createTime;

    public enum SyncType {
        local_to_remote, remote_to_local
    }

    public enum SyncMode {
        copy, sync
    }


    /**
     * 本地全路径转远端路径
     *
     * @return
     */
    public String localToRemotePath(String fullLocalPath) {
        return PathUtil.pathJoin(getRootRemotePath(), localToPath(fullLocalPath));
    }

    /**
     * 远端全路径转本地路径
     *
     * @return
     */
    public String remoteToLocalPath(String fullRemotePath) {
        return PathUtil.pathJoin(getLocalPath(), remoteToPath(fullRemotePath));
    }

    /**
     * 远端路径转共享路径
     *
     * @param fullRemotePath
     * @return
     */
    public String remoteToPath(String fullRemotePath) {
        return PathUtil.pathJoin(fullRemotePath.substring(getRootRemotePath().length()));
    }

    /**
     * 本地路径转共享路径
     *
     * @param fullLocalPath
     * @return
     */
    public String localToPath(String fullLocalPath) {
        return PathUtil.pathJoin(fullLocalPath.substring(localPath.length()));
    }


    /**
     * 获取远端根路径，有的URL里就带了路径也要拼接起来
     *
     * @return
     */
    public String getRootRemotePath() {
        int i = webDavAddress.indexOf("/", 9);
        if (i > 0) {
            return PathUtil.pathJoin(webDavAddress.substring(i),remotePath);
        }
        return remotePath;
    }
}
